package ist.swt.snippet.list;

import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class Snippet_List {

	/**
	 * @param args
	 */
	Display display = null;
	Shell shell = null;	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		(new Snippet_List()).execute();
	}	
	public Snippet_List() {
		// TODO Auto-generated constructor stub
		super();
		initialize();
	}	
	private void initialize()
	{
		display = new Display();		
		shell = new Shell(display, SWT.RESIZE | SWT.MIN);
		shell.setSize(400, 300);
		shell.setText("IST Java");	
		
		
		initialize_Shell();
	}		
	public void execute()
	{
		shell.open();	
		while (!shell.isDisposed()) 
		{
			if(!display.readAndDispatch())
				display.sleep();			
		}
		display.dispose();
	}
	
	
	
	private void initialize_Shell()
	{
		
		/**
		 * Lista 1
		 */
		Label label_lista1 = new Label(shell, SWT.NONE);
		label_lista1.setText("Lista 1");
		label_lista1.setBounds(10,10,50,20);
		
		
		
		/**
		 * SWT.SINGLE -> Seleccionar uno a la vez
		 * SWT.MULTI -> Seleccionar más de uno a la vez
		 */
		final List list_1 = new List(shell, 
				SWT.MULTI |
				SWT.BORDER);
		list_1.setBounds(10, 40, 200, 400);
		list_1.add("1");
		list_1.add("2");
		list_1.add("3");
		list_1.add("4");
		list_1.add("5");
		list_1.add("6");
		list_1.add("7");
		
		
		
		
		/**
		 * Lista 2
		 */
		Label label_lista2 = new Label(shell, SWT.NONE);
		label_lista2.setText("Lista 2");
		label_lista2.setBounds(260,10,50,20);
		
		
		
		/**
		 * SWT.SINGLE -> Seleccionar uno a la vez
		 * SWT.MULTI -> Seleccionar más de uno a la vez
		 */
		final List list_2 = new List(shell, 
				SWT.MULTI |
				SWT.BORDER);
		list_2.setBounds(260, 40, 200, 400);

		
		
		Button button_Right = new Button(shell, SWT.ARROW | SWT.RIGHT);
		button_Right.setBounds(220, 40, 30, 30);
		button_Right.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				int a[] = list_1.getSelectionIndices();

				for (int i : a) {
					list_2.add(list_1.getItem(i));
				}
				
				list_1.remove(a);
			}
		});
		
		
		
		Button button_Left = new Button(shell, SWT.ARROW | SWT.LEFT);
		button_Left.setBounds(220, 80, 30, 30);
		
		button_Left.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int a[] = list_2.getSelectionIndices();
				
				for (int i : a) {
					list_1.add(list_2.getItem(i));
				}
				
				list_2.remove(a);
			}
			
		});
		
	}
	
}
